import { getData, service } from '@vimi/utils-api'
import { SERVICE_NAME } from '@/gov/plugins/constant'
const govMessagePageSize = 20

export const getNoClaim = (addressCode, isRead, params) =>
  getData(
    SERVICE_NAME,
    'credit_message_no_claim_view',
    {
      address_code: addressCode,
      'is_read.eq': isRead,
      size: govMessagePageSize,
      ...params,
    },
    {
      N1approved_date: 'DESC',
    }
  )

export const getLoanConfuse = (addressCode, isRead, params) =>
  getData(
    SERVICE_NAME,
    'credit_message_loan_confuse_view',
    {
      address_code: addressCode,
      'is_read.eq': isRead,
      ...params,
    },
    {
      N1confuse_time: 'DESC',
    }
  )

export const getYesClaim = (addressCode, isRead, params) =>
  getData(
    SERVICE_NAME,
    'credit_message_yes_claim_view',
    {
      address_code: addressCode,
      'is_read.eq': isRead,
      ...params,
    },
    {
      N1claim_time: 'DESC',
    }
  )

export const getLoanLending = (addressCode, isRead, params) =>
  getData(
    SERVICE_NAME,
    'credit_message_loan_lending_view',
    {
      address_code: addressCode,
      'is_read.eq': isRead,
      ...params,
    },
    {
      N1load_date: 'DESC',
    }
  )

export const getLoanBehExc = (addressCode, isRead, params) =>
  getData(
    SERVICE_NAME,
    'credit_message_loan_beh_exc_view',
    {
      address_code: addressCode,
      'is_read.eq': isRead,
      ...params,
    },
    {
      N1visit_date: 'DESC',
    }
  )

export const updateMessageToRead = (userIds, msgKey, busMsgType = 3) =>
  service.put('/message/update_status', {
    userIds,
    msgKey,
    busMsgType,
  })

const state = {
  govMessagePageSize,
  govMessageNoClaim: [],
  govMessageNoClaimCount: 0,
  govMessageLoanConfuse: [],
  govMessageLoanConfuseCount: 0,
  govMessageYesClaim: [],
  govMessageYesClaimCount: 0,
  govMessageLoanLending: [],
  govMessageLoanLendingCount: 0,
  govMessageLoanBehExc: [],
  govMessageLoanBehExcCount: 0,
}

const getters = {
  govMessagePageSize: state => state.govMessagePageSize,
  govMessageNoClaim: state => state.govMessageNoClaim,
  govMessageNoClaimCount: state => state.govMessageNoClaimCount,
  govMessageLoanConfuse: state => state.govMessageLoanConfuse,
  govMessageLoanConfuseCount: state => state.govMessageLoanConfuseCount,
  govMessageYesClaim: state => state.govMessageYesClaim,
  govMessageYesClaimCount: state => state.govMessageYesClaimCount,
  govMessageLoanLending: state => state.govMessageLoanLending,
  govMessageLoanLendingCount: state => state.govMessageLoanLendingCount,
  govMessageLoanBehExc: state => state.govMessageLoanBehExc,
  govMessageLoanBehExcCount: state => state.govMessageLoanBehExcCount,
}

const actions = {
  async getGovMessageNoClaim({ commit, rootGetters }, params = {}) {
    const res = await getNoClaim(rootGetters.user.queryAddressCode, 0, params)
    const data = (res && res.data) || {}
    commit('govMessageNoClaim', data.data || [])
    commit('govMessageNoClaimCount', data.totalCount || 0)
  },
  async getGovMessageLoanConfuse({ commit, rootGetters }, params = {}) {
    const res = await getLoanConfuse(rootGetters.user.queryAddressCode, 0, params)
    const data = (res && res.data) || {}
    commit('govMessageLoanConfuse', data.data || [])
    commit('govMessageLoanConfuseCount', data.totalCount || 0)
  },
  async getGovMessageYesClaim({ commit, rootGetters }, params = {}) {
    const res = await getYesClaim(rootGetters.user.queryAddressCode, 0, params)
    const data = (res && res.data) || {}
    commit('govMessageYesClaim', data.data || [])
    commit('govMessageYesClaimCount', data.totalCount || 0)
  },
  async getGovMessageLoanLending({ commit, rootGetters }, params = {}) {
    const res = await getLoanLending(rootGetters.user.queryAddressCode, 0, params)
    const data = (res && res.data) || {}
    commit('govMessageLoanLending', data.data || [])
    commit('govMessageLoanLendingCount', data.totalCount || 0)
  },
  async getGovMessageLoanBehExc({ commit, rootGetters }, params = {}) {
    const res = await getLoanBehExc(rootGetters.user.queryAddressCode, 0, params)
    const data = (res && res.data) || {}
    commit('govMessageLoanBehExc', data.data || [])
    commit('govMessageLoanBehExcCount', data.totalCount || 0)
  },
  async updateGovMessageNoClaim({ rootGetters }) {
    const { userId, orgRegionLever } = rootGetters.user
    if (!state.govMessageNoClaimCount || orgRegionLever < 4) return
    await updateMessageToRead(userId, 'LOAN_NO_CLAIM')
    this.dispatch('getGovMessageNoClaim')
  },
  async updateGovMessageLoanConfuse({ rootGetters }) {
    const { userId, orgRegionLever } = rootGetters.user
    if (!state.govMessageLoanConfuseCount || orgRegionLever < 4) return
    await updateMessageToRead(userId, 'LOAN_CONFUSE')
    this.dispatch('getGovMessageLoanConfuse')
  },
  async updateGovMessageYesClaim({ rootGetters }) {
    const { userId, orgRegionLever } = rootGetters.user
    if (!state.govMessageYesClaimCount || orgRegionLever < 4) return
    await updateMessageToRead(userId, 'LOAN_YES_CLAIM')
    this.dispatch('getGovMessageYesClaim')
  },
  async updateGovMessageLoanLending({ rootGetters }) {
    const { userId, orgRegionLever } = rootGetters.user
    if (!state.govMessageLoanLendingCount || orgRegionLever < 4) return
    await updateMessageToRead(userId, 'LOAN_LENDING')
    this.dispatch('getGovMessageLoanLending')
  },
  async updateGovMessageLoanBehExc({ rootGetters }) {
    const { userId, orgRegionLever } = rootGetters.user
    if (!state.govMessageLoanBehExcCount || orgRegionLever < 4) return
    await updateMessageToRead(userId, 'LOAN_BEH_EXC')
    this.dispatch('getGovMessageLoanBehExc')
  },
}

const mutations = {
  govMessageNoClaim: (state, payload) => (state.govMessageNoClaim = payload),
  govMessageNoClaimCount: (state, payload) => (state.govMessageNoClaimCount = payload),
  govMessageLoanConfuse: (state, payload) => (state.govMessageLoanConfuse = payload),
  govMessageLoanConfuseCount: (state, payload) => (state.govMessageLoanConfuseCount = payload),
  govMessageYesClaim: (state, payload) => (state.govMessageYesClaim = payload),
  govMessageYesClaimCount: (state, payload) => (state.govMessageYesClaimCount = payload),
  govMessageLoanLending: (state, payload) => (state.govMessageLoanLending = payload),
  govMessageLoanLendingCount: (state, payload) => (state.govMessageLoanLendingCount = payload),
  govMessageLoanBehExc: (state, payload) => (state.govMessageLoanBehExc = payload),
  govMessageLoanBehExcCount: (state, payload) => (state.govMessageLoanBehExcCount = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
